import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SyntaxTest extends JFrame {

	private JPanel contentPane;
	private JTextArea textCode;
	private JLabel lblResult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SyntaxTest frame = new SyntaxTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SyntaxTest() {
		setTitle("Syntax Test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textCode = new JTextArea();
		textCode.setBounds(14, 38, 404, 139);
		contentPane.add(textCode);

		JButton btnTest = new JButton("Test");
		btnTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnTestCall();
			}
		});
		btnTest.setBounds(159, 187, 113, 27);
		contentPane.add(btnTest);

		lblResult = new JLabel("Input code!");
		lblResult.setHorizontalAlignment(SwingConstants.CENTER);
		lblResult.setBounds(14, 224, 404, 18);
		contentPane.add(lblResult);

		JLabel lblCode = new JLabel("code:");
		lblCode.setHorizontalAlignment(SwingConstants.LEFT);
		lblCode.setBounds(14, 10, 404, 18);
		contentPane.add(lblCode);
	}

	protected void btnTestCall() {
		if (textCode.getText().length() < 1) {
			lblResult.setText("Input some code!");
			return;
		}
		String code = textCode.getText();
		if (isLegal(code)) {
			lblResult.setText("Code is legal!");
		} else {
			lblResult.setText("Code is illegal!");
		}
	}

	public int vertifyFlag(String str) {
		switch (str) {
		case "(":
		case "{":
		case "/*":
			return -1;
		case ")":
		case "}":
		case "*/":
			return 1;
		default:
			break;
		}
		return 0;
	}

	public boolean isMatch(String leftflag, String rightflag) {
		switch (leftflag) {
		case "(":
			if (")".equals(rightflag)) {
				return true;
			}
			break;
		case "{":
			if ("}".equals(rightflag)) {
				return true;
			}
			break;
		case "/*":
			if ("*/".equals(rightflag)) {
				return true;
			}
			break;
		default:
			break;
		}
		return false;
	}

	public boolean isLegal(String code) {
		boolean result = true;
		LinkStack stack = new LinkStack();
		for (int i = 0; i < code.length(); i++) {
			String c = code.substring(i, i + 1);
			if ("/".equals(c) || "*".equals(c)) {
				if (i + 1 < code.length()) {
					c = code.substring(i, i + 2);
				}
			}
			if (this.vertifyFlag(c) < 0) {
				stack.push(c);
			} else if (this.vertifyFlag(c) > 0) {
				String leftFlag = (String) stack.peek();
				if (!isMatch(leftFlag, c)) {
					result = false;
					break;
				} else {
					try {
						stack.pop();
					} catch (Exception e) {
						result = false;
						break;
					}
				}
			}
		}
		if (!stack.isEmpty()) {
			result = false;
		}
		return result;
	}
}
